package com.aditya.personalexpense.dataaccess;

import java.util.Vector;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import com.aditya.personalexpense.PersonalExpensesApplication;
import com.aditya.personalexpense.database.DatabaseHelper;
import com.aditya.personalexpense.dataobject.ExpenceDO;

public class ExpencesDA 
{

	public long insertExpences(ExpenceDO expenceDO)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
		
					 SQLiteStatement stmt    = database.compileStatement("INSERT INTO Expences (amount, description, date, cid, pid, curencyid) VALUES (?,?,?,?,?,?)");
					 stmt.bindLong(1, expenceDO.amount);
					 stmt.bindString(2, expenceDO.description);
					 stmt.bindString(3, expenceDO.date);
					 stmt.bindLong(4, expenceDO.cid);
					 stmt.bindLong(5, expenceDO.pid);
					 stmt.bindLong(6, expenceDO.curencyid);
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("Expences", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	public long  updateExpences(ExpenceDO expenceDO)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
						
					 //1- true , 0 -false
					 SQLiteStatement stmt = database.compileStatement("update  Expences set amount= '"+expenceDO.amount+"', description ='"+expenceDO.description+"'date = '"+expenceDO.date+"'cid = "+expenceDO.cid+"pid = "+expenceDO.pid+"curencyid="+expenceDO.curencyid+" where cid="+expenceDO.eid);
					 
					 appID = stmt.executeInsert();
					 stmt.close();
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("update", "Expences : "+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return appID;
		}
	}
	
	
	public ExpenceDO getExpencesById(int eid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 ExpenceDO expenceDO = null;
			 String query = "SELECT * FROM Expences WHERE eid=?";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, new String[]{""+eid});
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 expenceDO = new ExpenceDO();
							 expenceDO.eid = cursor.getLong(0);
							 expenceDO.amount = cursor.getLong(1);
							 expenceDO.description = cursor.getString(2);
							 expenceDO.date = cursor.getString(3);
							 expenceDO.cid = cursor.getLong(4);
							 expenceDO.pid = cursor.getLong(5);
							 expenceDO.curencyid = cursor.getLong(6);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("Expences", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return expenceDO;
		}
		
	}
	
	
	

	public Vector<ExpenceDO> getAllExpences()
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{ 
			 long appID = -1;
			 SQLiteDatabase database =null;
			 Vector<ExpenceDO> vecExpenceDO = new Vector<ExpenceDO>();
			 String query = "SELECT * FROM Expences";
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
					 //1- true , 0 -false
					 Cursor cursor = database.rawQuery(query, null);
					 if(cursor!=null && cursor.moveToFirst())
					 {
						 do
						 {
							 ExpenceDO expenceDO = new ExpenceDO();
							 expenceDO.eid = cursor.getLong(0);
							 expenceDO.amount = cursor.getLong(1);
							 expenceDO.description = cursor.getString(2);
							 expenceDO.date = cursor.getString(3);
							 expenceDO.cid = cursor.getLong(3);
							 expenceDO.pid = cursor.getLong(3);
							 expenceDO.curencyid = cursor.getLong(3);
							 vecExpenceDO.add(expenceDO);
							 
						 }while(cursor.moveToNext());
					 }
					
				 }
			 }
			 catch(Exception e)
			 {
				 Log.e("tblParentalCntrolGeneralSettings", ""+e);
				 e.printStackTrace();
			 }
			 finally
			 {
				 if(database!=null && database.isOpen())
					 database.close();
			 }
				
			return vecExpenceDO;
		}
	
		
	}
	

	public void deleteExpencesById(int eid)
	{
		synchronized (PersonalExpensesApplication.MyLock) 
		{
			 SQLiteDatabase database =null;
			 try
			 {
				 database  =  DatabaseHelper.openDataBase();
				 if(database != null)
				 {
	
					 String whereClause = "eid=?";
					 database.delete("Expences", whereClause, new String[]{""+eid});
				 }
			 }
			 catch(Exception e)
			 {
				 e.printStackTrace();
			 }
			 finally
			 {
				 
				 if(database!=null && database.isOpen())
					 database.close();
			 }
		}
		 
	}
	
	


}
